// pages/homelist/homelist.js
//获取应用实例
var app = getApp();
//引用
var fileData = require('../../utils/data.js');
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    actiItem: [],
    userInfo: '',
    type: 3,
    option: {},
    currentPage: 1,
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: true,
    loadSuccess: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      option: options
    })
    // this.onShow(options);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  enterPerson: function (e) {
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id=' + e.target.id
    })
  },
  getData: function (flag) {//我的活动
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getReleasedActivitiesWithUserIdByPage', //仅为示例，并非真实的接口地址
      data: {
        userId: _this.data.userInfo.id,
        currentPage: this.data.currentPage,
        pageNumber: '10',
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.activities) {
          if (typeof res.data.data.activities != 'string') {
            if (res.data.data.activities.length < 10) {
              _this.setData({
                hasData: false
              })
            } else {
              _this.setData({
                hasData: true
              })
            }
            res.data.data.activities.forEach(function (ele) {
              // for (var j in ele.tGyActivity) {
              //   ele[j] = ele.tGyActivity[j];
              // }
              if (ele.releaseTime) {
                ele.time = util.getDays(new Date(ele.releaseTime));
              }
            })
            if (flag) {
              wx.hideNavigationBarLoading() //完成停止加载
              wx.stopPullDownRefresh() //停止下拉刷新
              _this.setData({
                actiItem: []
              });
            } else {
              _this.setData({
                loadMore: false
              });
            }
            _this.data.actiItem = _this.data.actiItem.concat(res.data.data.activities);
            _this.setData({
              actiItem: _this.data.actiItem
            })
          }
        } else {
          _this.setData({
            loadMore: false,
            hasData: false
          });
        }
      }
    })
  },
  getMyData(flag) {//我的报名
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getJoinedActivitiesWithUserIdByPage', //仅为示例，并非真实的接口地址
      data: {
        userId: _this.data.userInfo.id,
        currentPage: this.data.currentPage,
        pageNumber: '10',
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.activities) {
          if (typeof res.data.data.activities != 'string') {
            if (res.data.data.activities.length < 10) {
              _this.setData({
                hasData: false
              })
            } else {
              _this.setData({
                hasData: true
              })
            }
            res.data.data.activities.forEach(function (ele) {
              if (ele.releaseTime) {
                ele.time = util.getDays(new Date(ele.releaseTime));
                // ele.time = util.getDays(new Date(ele.releaseTime).getTime());
              }
            })
            if (flag) {
              wx.hideNavigationBarLoading() //完成停止加载
              wx.stopPullDownRefresh() //停止下拉刷新
              _this.setData({
                actiItem: []
              });
            } else {
              _this.setData({
                loadMore: false
              });
            }
            _this.data.actiItem = _this.data.actiItem.concat(res.data.data.activities);
            _this.setData({
              actiItem: _this.data.actiItem
            })
          }
        } else {
          _this.setData({
            loadMore: false,
            hasData: false
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (data) {
    this.setData({
      loadSuccess: false
    })
    wx.showLoading();
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    if (this.data.option.isMine) {
      wx.setNavigationBarTitle({
        title: '我的活动',
      })
      this.getData(true);
      this.setData({
        type: 2
      })
    } else {
      this.getMyData(true);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      currentPage: 1
    })
    if (this.data.type == 2) {
      this.getData(true);
    } else {
      this.getMyData(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasData) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        currentPage: this.data.currentPage + 1
      });
      if (this.data.type == 2) {
        this.getData(true);
      } else {
        this.getMyData(true);
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})